﻿// Authors: Manuel de la Peña <manuel.delapena@canonical.com>
using NUnit.Framework;

namespace DotUpdater.Test
{
    /// <summary>
    /// Tests the different algorihms used to hash a 
    /// </summary>
    [TestFixture]    
    public class ChecksumAlgorithmsFixture
    {

        #region Variables

        private string _firstFilePath;
        private string _secondFilePath;

        #endregion

        #region Setup

        [SetUp]
        public void Setup()
        {
            _firstFilePath = "../../ChecksumAlgorithmsFixture.cs";
            _secondFilePath = "../../TestDownloader.cs";
        }

        #endregion

        #region Tests

        [Test]
        public void Md5Base64Test()
        {
            // calculate twice the checksum and ensure they are the same
            var firstChecksum = ChecksumAlgorithms.Md5(_firstFilePath);
            var secondChecksum = ChecksumAlgorithms.Md5(_firstFilePath);
            Assert.AreEqual(secondChecksum, firstChecksum);
            // calcualte the checum of diff files and ensure tey are diff
            secondChecksum = ChecksumAlgorithms.Md5(_secondFilePath);
            Assert.AreNotEqual(secondChecksum, firstChecksum);
        }

        [Test]
        public void Sha1Test()
        {
            // calculate twice the checksum and ensure they are the same
            var firstChecksum = ChecksumAlgorithms.Sha1(_firstFilePath);
            var secondChecksum = ChecksumAlgorithms.Sha1(_firstFilePath);
            Assert.AreEqual(secondChecksum, firstChecksum);
            // calcualte the checum of diff files and ensure tey are diff
            secondChecksum = ChecksumAlgorithms.Sha1(_secondFilePath);
            Assert.AreNotEqual(secondChecksum, firstChecksum);
        }

        [Test]
        public  void Sha256Test()
        {
            // calculate twice the checksum and ensure they are the same
            var firstChecksum = ChecksumAlgorithms.Sha256(_firstFilePath);
            var secondChecksum = ChecksumAlgorithms.Sha256(_firstFilePath);
            Assert.AreEqual(secondChecksum, firstChecksum);
            // calcualte the checum of diff files and ensure tey are diff
            secondChecksum = ChecksumAlgorithms.Sha256(_secondFilePath);
            Assert.AreNotEqual(secondChecksum, firstChecksum);
        }

        [Test]
        public void Sha384Test()
        {
            // calculate twice the checksum and ensure they are the same
            var firstChecksum = ChecksumAlgorithms.Sha384(_firstFilePath);
            var secondChecksum = ChecksumAlgorithms.Sha384(_firstFilePath);
            Assert.AreEqual(secondChecksum, firstChecksum);
            // calcualte the checum of diff files and ensure tey are diff
            secondChecksum = ChecksumAlgorithms.Sha384(_secondFilePath);
            Assert.AreNotEqual(secondChecksum, firstChecksum);
        }

        [Test]
        public void Sha512Test()
        {
            // calculate twice the checksum and ensure they are the same
            var firstChecksum = ChecksumAlgorithms.Sha512(_firstFilePath);
            var secondChecksum = ChecksumAlgorithms.Sha512(_firstFilePath);
            Assert.AreEqual(secondChecksum, firstChecksum);
            // calcualte the checum of diff files and ensure tey are diff
            secondChecksum = ChecksumAlgorithms.Sha512(_secondFilePath);
            Assert.AreNotEqual(secondChecksum, firstChecksum);
        }

        #endregion
    }
}
